<template>
  <a href="javascript:;" :class="className" @click.prevent="submit">
    <slot></slot>
    <form method="post" :action="action" class="hide">
      <input type="hidden" name="_token" :value="csrfToken">
      <input type="hidden" name="_method" :value="method">
    </form>
  </a>
</template>

<script>
  export default {
    props: {
      action: {
        type: String,
        require: true
      },
      className: {
        type: String,
        default: ""
      },
      method: {
        type: String,
        default: "post",
        validator: function (value) {
          return ['post', 'put', 'delete'].indexOf(value.toLowerCase()) !== -1;
        }
      }
    },
    data() {
      return {
        csrfToken: $('meta[name="csrf-token"]').attr('content')
      }
    },
    methods: {
      submit: function (e) {

        if (this.method.toLowerCase() === 'delete') {
          if (! confirm('是否确认删除该记录？')) {
            return false;
          }
        }

        $(e.target).next('form').submit();
      }
    }
  }
</script>